package com.meizi.utils;

/**
 * @author Administration
 * @date 2021/8/9 - 14:22 - 星期一
 * @project lambda
 * @package com.meizi.utils
 * @by GGbomd猪猪侠
 */

import com.meizi.enums.ErrorCodeEnum;

import java.io.Serializable;

/**
 * 返回值工具类
 * @author Administration
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private int code = 0;
    private String msg = "success";
    private T data;

    public Result<T> ok(T data) {
        this.setData(data);
        return this;
    }

    public Result<T> error() {
        this.code = ErrorCodeEnum.INTERNAL_SERVER_ERROR.getKey();
        this.msg = "未知错误";
        this.setData((T) null);
        return this;
    }

    public Result<T> error(int code) {
        this.code = code;
        this.msg = ErrorCodeUtils.getMsg(code);
        this.setData((T) null);
        return this;
    }

    public Result<T> error(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.setData((T) null);
        return this;
    }

    public Result<T> errorInfo(int code, String msg) {
        this.code = code;
        this.msg = ErrorCodeUtils.getMsg(code, msg);
        this.setData((T) null);
        return this;
    }

    public Result<T> error(String msg) {
        this.code = ErrorCodeEnum.INTERNAL_SERVER_ERROR.getKey();
        this.msg = msg;
        this.setData((T) null);
        return this;
    }

    public Result<T> error(String msg, T data) {
        this.code = ErrorCodeEnum.INTERNAL_SERVER_ERROR.getKey();
        this.msg = msg;
        this.setData(data);
        return this;
    }

    public boolean success() {
        return this.code == 0;
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Result)) {
            return false;
        } else {
            Result<?> other = (Result)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getCode() != other.getCode()) {
                return false;
            } else {
                Object this$msg = this.getMsg();
                Object other$msg = other.getMsg();
                if (this$msg == null) {
                    if (other$msg != null) {
                        return false;
                    }
                } else if (!this$msg.equals(other$msg)) {
                    return false;
                }

                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof Result;
    }

    @Override
    public String toString() {
        return "Result(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
    }

    public Result(final int code, final String msg, final T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result() {
    }
}
